package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.adapter.PurGoodsGridAdapter;
import com.aoxon.cargo.adapter.PurSupplierListAdapter;
import com.aoxon.cargo.bean.Cloth;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.cache.PurCache;
import com.aoxon.cargo.component.MyLoadCloth;
import com.aoxon.cargo.component.MyRadioButton;
import com.aoxon.cargo.component.WaterFallShowGoods;
import com.aoxon.cargo.database.DataService;
import com.aoxon.cargo.keydown.ExitApplication;
import com.aoxon.cargo.service.ClickGoodsService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.shared.CollectGoodsShared;
import com.aoxon.cargo.shared.CollectSupplierShared;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ExecutorUtil;
//import com.cnzz.mobile.android.sdk.MobileProbe;

/**
 * 采购端物品收藏显示页面
 * 
 * @author ZZJ
 * 
 */
public class PurCollectionActivity extends BaseActivity implements
		MyRadioButton.OnCheckedListener {
	private String tag = "PurCollectionActivity";
	
	public static int OPEN_GOODS_RESULT = 0;
	public static int OPEN_SUPPLIER_RESULT = 1;
	private String deviceId = null;

	private ImageView ivReturnButton;
	private ImageView ivDeleteButton;
	private TextView tvTitle;
	private TextView tvTips;

	private ListView lvColSupplierList;

	private WaterFallShowGoods wShowGoods;
	private MyRadioButton myRadioButton;

	private PurGoodsGridAdapter gAdapter;
	private PurSupplierListAdapter sAdapter;

	private ExitApplication exitApplication = new ExitApplication(this);

	private Supplier supplier;
	private DataService dataService = new DataService(this);
	private List<Cloth> cloths = new ArrayList<Cloth>();
	private List<Supplier> suppliers = new ArrayList<Supplier>();
	private ClickGoodsService clickGoodsService = (ClickGoodsService) MyServiceFactory
			.getInstance(MyServiceFactory.CLICK_GOODS);

	private boolean showGoods = true;// 用于判断显示类型以进行删除操作

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setup();

		showGoods();

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
//		MobileProbe.onPause(this);
		super.onPause();
	}
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private void setup() {
		setContentView(R.layout.pur_collection);

		supplier = SupIndexShared.getSupplier();
		try {
			TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
			deviceId = telMgr.getDeviceId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		ivDeleteButton = (ImageView) findViewById(R.id.ivPublicTitleOther);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvTips = (TextView) findViewById(R.id.tvPurCollectionText);

		lvColSupplierList = (ListView) findViewById(R.id.lvPurCollectionSuppliers);

		myRadioButton = new MyRadioButton(this, "商品", "商家");
		myRadioButton.setOnCheckedChanged(this);

		gAdapter = new PurGoodsGridAdapter(this, cloths, true);
		sAdapter = new PurSupplierListAdapter(this, suppliers, true);
		wShowGoods = new WaterFallShowGoods(this, gAdapter, cloths);
		lvColSupplierList.setAdapter(sAdapter);
		lvColSupplierList.setOnItemClickListener(onSupplierItemClickListener);

		ivReturnButton.setVisibility(View.GONE);
		ivDeleteButton.setImageResource(R.drawable.button_delete_collection);
		tvTitle.setText("收藏");

		wShowGoods.getGridView().setNumColumns(3);
		wShowGoods.getGridView().setOnItemClickListener(
				onGoodsItemClickListener);
		wShowGoods.setToLoad(toLoad);

		ivDeleteButton.setOnClickListener(myClickListener);
		wShowGoods.setProgressFlag(View.GONE);
	}

	private void showGoods() {
		cloths.clear();
//		List<Cloth> clothList = CollectGoodsShared.getColGoodsList();
		List<Cloth> clothList = null;
//		if(supplier!=null){
//			 clothList = dataService.getClothes(supplier.getSupId(),
//					0, 0, MyStateUtil.COLLECTION_CLOTH);
//		}else{
			clothList = dataService.getClothes(deviceId,
					0, 0, MyStateUtil.COLLECTION_CLOTH);
//		}
		
		if (clothList == null || clothList.size() == 0) {
			tvTips.setText("你还没收藏任何商品喔～");
			tvTips.setVisibility(View.VISIBLE);
		} else {
			tvTips.setVisibility(View.GONE);
			wShowGoods.setListFlag(View.VISIBLE);
			cloths.addAll(clothList);
		}
		gAdapter.notifyDataSetChanged();
	}

	private void showSupplier() {
		suppliers.clear();
//		List<Supplier> supplierList = CollectSupplierShared
//				.getColSupplierList();
		List<Supplier> supplierList = null;
//		if(supplier!=null){
			 supplierList = dataService.getSuppliers(deviceId,
					0, 0, MyStateUtil.COLLECTION_SUPPLIER);
//		}
		if (supplierList == null || supplierList.size() == 0) {
			Log.v(tag, "null");
			tvTips.setText("你还没收藏任何商家喔～");
			tvTips.setVisibility(View.VISIBLE);
		} else {
			tvTips.setVisibility(View.GONE);
			lvColSupplierList.setVisibility(View.VISIBLE);
			suppliers.addAll(supplierList);
		}
		sAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (OPEN_GOODS_RESULT == requestCode) {
			cloths.clear();
			wShowGoods.clearView();
			showGoods();
		} else {
			showSupplier();
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return exitApplication.onKeyDown(keyCode, event);
	}

	@Override
	public void checkOne() {
		// TODO Auto-generated method stub
		showGoods = true;
		lvColSupplierList.setVisibility(View.GONE);
		wShowGoods.setListFlag(View.VISIBLE);
		showGoods();
	}

	@Override
	public void checkTwo() {
		// TODO Auto-generated method stub
		showGoods = false;
		wShowGoods.setListFlag(View.GONE);
		lvColSupplierList.setVisibility(View.VISIBLE);
		showSupplier();
	}

	OnClickListener myClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			
			// TODO Auto-generated method stub
			if (showGoods) {
				if (gAdapter.deleteCheck()) {
//					CollectGoodsShared.saveColGoodsList(cloths);
					showGoods();
				}
			} else {
				if (sAdapter.deleteCheck()) {
//					CollectSupplierShared.saveColSupplierList(suppliers);
					showSupplier();
				}
			}
		}
	};

	OnItemClickListener onGoodsItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			PurCache.cloth = cloths.get(position);
			final int clothsId = cloths.get(position).getClothId();
			ExecutorUtil.getCachedExecutorService().submit(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					clickGoodsService.execute(clothsId);
				}
			});
			Intent intent = new Intent(PurCollectionActivity.this,
					PurGoodsPagerActivity.class);
			MyLoadCloth.setExtra(intent, wShowGoods.startIndex,
					wShowGoods.length, MyLoadCloth.COLLECT, position, cloths);
			startActivityForResult(intent, OPEN_GOODS_RESULT);
		}
	};

	OnItemClickListener onSupplierItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getBaseContext(),
					PurSupplierDetailsActivity.class);
			intent.putExtra("strSupplier",
					DataUtil.gson.toJson(suppliers.get(position)));
			startActivityForResult(intent, OPEN_SUPPLIER_RESULT);
		}

	};

	WaterFallShowGoods.ToLoad toLoad = new WaterFallShowGoods.ToLoad() {
		@Override
		public boolean doLoad() throws Exception {
			// TODO Auto-generated method stub
			return false;
		}
	};
}
